﻿@using System.Linq
@model IEnumerable<Consumer.Models.Course>

@{
    ViewBag.Title = "Courses";
}

<h2>@ViewBag.Title</h2>

@if (User.IsInRole(Consumer.Models.UserRoles.TeacherRole))
{
    <p>
        @Html.ActionLink("Create a Course", "Create")
    </p>
}
else
{
    <p>
        You must have the Teacher role to create or edit courses. To change your roles, visit your
        Profile page.
    </p>
}

@if (!Model.Any())
{
    <p>
        There are no courses yet.
    </p>
}
else
{
    <table class="table">
        <tr>
            <th>@Html.DisplayNameFor(model => model.Name)</th>
            <th>@Html.DisplayNameFor(model => model.Instructor)</th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @item.Instructor.FullName
                </td>
                <td>
                    @Html.ActionLink("Details", "Details", new { id = item.CourseId })
                    @if (User.IsInRole(Consumer.Models.UserRoles.TeacherRole) && item.Instructor.Id == ViewBag.UserId)
                    {
                        <text> | </text>@Html.ActionLink("Edit", "Edit", new { id = item.CourseId })
                        <text> | </text>@Html.ActionLink("Delete", "Delete", new { id = item.CourseId })
                    }
                </td>
            </tr>
        }

    </table>
}

